home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / man / cat.1 / perlform.1 < prev    next >
Text File  |  1995-07-25  |  19KB  |  463 lines

  1.  
  2.  
  3.  
  4.      PPPPEEEERRRRLLLLFFFFOOOORRRRMMMM((((1111)))) UUUUNNNNIIIIXXXX SSSSyyyysssstttteeeemmmm VVVV ((((RRRReeeelllleeeeaaaasssseeee 0000....0000 PPPPaaaattttcccchhhhlllleeeevvvveeeellll 00000000)))) PPPPEEEERRRRLLLLFFFFOOOORRRRMMMM((((1111))))
  5.  
  6.  
  7.  
  8.      NNNNAAAAMMMMEEEE
  9.           perlform - Perl formats
  10.  
  11.      DDDDEEEESSSSCCCCRRRRIIIIPPPPTTTTIIIIOOOONNNN
  12.           Perl has a mechanism to help you generate simple reports and
  13.           charts.  To facilitate this, Perl helps you lay out your
  14.           output page in your code in a fashion that's close to how it
  15.           will look when it's printed.  It can keep track of things
  16.           like how many lines on a page, what page you're, when to
  17.           print page headers, etc.  The keywords used are borrowed
  18.           from FORTRAN: _f_o_r_m_a_t() to declare and _w_r_i_t_e() to execute;
  19.           see their entries in the _m_a_n_f_u_n_c manpage.  Fortunately, the
  20.           layout is much more legible, more like BASIC's PRINT USING
  21.           statement.  Think of it as a poor man's _n_r_o_f_f(1).
  22.  
  23.           Formats, like packages and subroutines, are declared rather
  24.           than executed, so they may occur at any point in your
  25.           program.  (Usually it's best to keep them all together
  26.           though.) They have their own namespace apart from all the
  27.           other "types" in Perl.  This means that if you have a
  28.           function named "Foo", it is not the same thing as having a
  29.           format named "Foo".  However, the default name for the
  30.           format associated with a given filehandle is the same as the
  31.           name of the filehandle.  Thus, the default format for STDOUT
  32.           is name "STDOUT", and the default format for filehandle TEMP
  33.           is name "TEMP".  They just look the same.  They aren't.
  34.  
  35.           Output record formats are declared as follows:
  36.  
  37.               format NAME =
  38.               FORMLIST
  39.               .
  40.  
  41.           If name is omitted, format "STDOUT" is defined.  FORMLIST
  42.           consists of a sequence of lines, each of which may be of one
  43.           of three types:
  44.  
  45.           1.  A comment, indicated by putting a '#' in the first
  46.               column.
  47.  
  48.           2.  A "picture" line giving the format for one output line.
  49.  
  50.           3.  An argument line supplying values to plug into the
  51.               previous picture line.
  52.  
  53.           Picture lines are printed exactly as they look, except for
  54.           certain fields that substitute values into the line.  Each
  55.           field in a picture line starts with either "@" (at) or "^"
  56.           (caret).  These lines do not undergo any kind of variable
  57.           interpolation.  The at field (not to be confused with the
  58.           array marker @) is the normal kind of field; the other kind,
  59.           caret fields, are used to do rudimentary multi-line text
  60.  
  61.  
  62.  
  63.      Page 1                                          (printed 6/30/95)
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70.      PPPPEEEERRRRLLLLFFFFOOOORRRRMMMM((((1111)))) UUUUNNNNIIIIXXXX SSSSyyyysssstttteeeemmmm VVVV ((((RRRReeeelllleeeeaaaasssseeee 0000....0000 PPPPaaaattttcccchhhhlllleeeevvvveeeellll 00000000)))) PPPPEEEERRRRLLLLFFFFOOOORRRRMMMM((((1111))))
  71.  
  72.  
  73.  
  74.           block filling.  The length of the field is supplied by
  75.           padding out the field with multiple "<", ">", or "|"
  76.           characters to specify, respectively, left justification,
  77.           right justification, or centering.  If the variable would
  78.           exceed the width specified, it is truncated.
  79.  
  80.           As an alternate form of right justification, you may also
  81.           use "#" characters (with an optional ".") to specify a
  82.           numeric field.  This way you can line up the decimal points.
  83.           If any value supplied for these fields contains a newline,
  84.           only the text up to the newline is printed.  Finally, the
  85.           special field "@*" can be used for printing multi-line,
  86.           non-truncated values; it should appear by itself on a line.
  87.  
  88.           The values are specified on the following line in the same
  89.           order as the picture fields.  The expressions providing the
  90.           values should be separated by commas.  The expressions are
  91.           all evaluated in a list context before the line is
  92.           processed, so a single list expression could produce
  93.           multiple list elements.  The expressions may be spread out
  94.           to more than one line if enclosed in braces.  If so, the
  95.           opening brace must be the first token on the first line.
  96.  
  97.           Picture fields that begin with ^ rather than @ are treated
  98.           specially.  With a # field, the field is blanked out if the
  99.           value is undefined.  For other field types, the caret
  100.           enables a kind of fill mode.  Instead of an arbitrary
  101.           expression, the value supplied must be a scalar variable
  102.           name that contains a text string.  Perl puts as much text as
  103.           it can into the field, and then chops off the front of the
  104.           string so that the next time the variable is referenced,
  105.           more of the text can be printed.  (Yes, this means that the
  106.           variable itself is altered during execution of the _w_r_i_t_e()
  107.           call, and is not returned.)  Normally you would use a
  108.           sequence of fields in a vertical stack to print out a block
  109.           of text.  You might wish to end the final field with the
  110.           text "...", which will appear in the output if the text was
  111.           too long to appear in its entirety.  You can change which
  112.           characters are legal to break on by changing the variable $:
  113.           (that's $FORMAT_LINE_BREAK_CHARACTERS if you're using the
  114.           English module) to a list of the desired characters.
  115.  
  116.           Since use of caret fields can produce variable length
  117.           records.  If the text to be formatted is short, you can
  118.           suppress blank lines by putting a "~" (tilde) character
  119.           anywhere in the line.  The tilde will be translated to a
  120.           space upon output.  If you put a second tilde contiguous to
  121.           the first, the line will be repeated until all the fields on
  122.           the line are exhausted.  (If you use a field of the at
  123.           variety, the expression you supply had better not give the
  124.           same value every time forever!)
  125.  
  126.  
  127.  
  128.  
  129.      Page 2                                          (printed 6/30/95)
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136.      PPPPEEEERRRRLLLLFFFFOOOORRRRMMMM((((1111)))) UUUUNNNNIIIIXXXX SSSSyyyysssstttteeeemmmm VVVV ((((RRRReeeelllleeeeaaaasssseeee 0000....0000 PPPPaaaattttcccchhhhlllleeeevvvveeeellll 00000000)))) PPPPEEEERRRRLLLLFFFFOOOORRRRMMMM((((1111))))
  137.  
  138.  
  139.  
  140.           Top-of-form processing is by default handled by a format
  141.           with the same name as the current filehandle with "_TOP"
  142.           concatenated to it.  It's triggered at the top of each page.
  143.           See <perlfunc/_w_r_i_t_e()>.
  144.  
  145.           Examples:
  146.  
  147.            # a report on the /etc/passwd file
  148.            format STDOUT_TOP =
  149.                                    Passwd File
  150.            Name                Login    Office   Uid   Gid Home
  151.            ------------------------------------------------------------------
  152.            .
  153.            format STDOUT =
  154.            @<<<<<<<<<<<<<<<<<< @||||||| @<<<<<<@>>>> @>>>> @<<<<<<<<<<<<<<<<<
  155.            $name,              $login,  $office,$uid,$gid, $home
  156.            .
  157.  
  158.            # a report from a bug report form
  159.            format STDOUT_TOP =
  160.                                    Bug Reports
  161.            @<<<<<<<<<<<<<<<<<<<<<<<     @|||         @>>>>>>>>>>>>>>>>>>>>>>>
  162.            $system,                      $%,         $date
  163.            ------------------------------------------------------------------
  164.            .
  165.            format STDOUT =
  166.            Subject: @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
  167.                     $subject
  168.            Index: @<<<<<<<<<<<<<<<<<<<<<<<<<<<< ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<
  169.                   $index,                       $description
  170.            Priority: @<<<<<<<<<< Date: @<<<<<<< ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<
  171.                      $priority,        $date,   $description
  172.            From: @<<<<<<<<<<<<<<<<<<<<<<<<<<<<< ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<
  173.                  $from,                         $description
  174.            Assigned to: @<<<<<<<<<<<<<<<<<<<<<< ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<
  175.                         $programmer,            $description
  176.            ~                                    ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<
  177.                                                 $description
  178.            ~                                    ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<
  179.                                                 $description
  180.            ~                                    ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<
  181.                                                 $description
  182.            ~                                    ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<
  183.                                                 $description
  184.            ~                                    ^<<<<<<<<<<<<<<<<<<<<<<<...
  185.                                                 $description
  186.            .
  187.  
  188.           It is possible to intermix _p_r_i_n_t()s with _w_r_i_t_e()s on the
  189.           same output channel, but you'll have to handle $-
  190.           ($FORMAT_LINES_LEFT) yourself.
  191.  
  192.  
  193.  
  194.  
  195.      Page 3                                          (printed 6/30/95)
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
  202.      PPPPEEEERRRRLLLLFFFFOOOORRRRMMMM((((1111)))) UUUUNNNNIIIIXXXX SSSSyyyysssstttteeeemmmm VVVV ((((RRRReeeelllleeeeaaaasssseeee 0000....0000 PPPPaaaattttcccchhhhlllleeeevvvveeeellll 00000000)))) PPPPEEEERRRRLLLLFFFFOOOORRRRMMMM((((1111))))
  203.  
  204.  
  205.  
  206.           FFFFoooorrrrmmmmaaaatttt VVVVaaaarrrriiiiaaaabbbblllleeeessss
  207.  
  208.           The current format name is stored in the variable $~
  209.           ($FORMAT_NAME), and the current top of form format name is
  210.           in $^ ($FORMAT_TOP_NAME).  The current output page number is
  211.           stored in $% ($FORMAT_PAGE_NUMBER), and the number of lines
  212.           on the page is in $= ($FORMAT_LINES_PER_PAGE).  Whether to
  213.           autoflush output on this handle is stored in $<$|>
  214.           ($OUTPUT_AUTOFLUSH).  The string output before each top of
  215.           page (except the first) is stored in $^L ($FORMAT_FORMFEED).
  216.           These variables are set on a per-filehandle basis, so you'll
  217.           need to _s_e_l_e_c_t() into a different one to affect them:
  218.  
  219.               select((select(OUTF),
  220.                       $~ = "My_Other_Format",
  221.                       $^ = "My_Top_Format"
  222.                      )[0]);
  223.  
  224.           Pretty ugly, eh?  It's a common idiom though, so don't be
  225.           too surprised when you see it.  You can at least use a
  226.           temporary variable to hold the previous filehandle: (this is
  227.           a much better approach in general, because not only does
  228.           legibility improve, you now have intermediary stage in the
  229.           expression to single-step the debugger through):
  230.  
  231.               $ofh = select(OUTF);
  232.               $~ = "My_Other_Format";
  233.               $^ = "My_Top_Format";
  234.               select($ofh);
  235.  
  236.           If you use the English module, you can even read the
  237.           variable names:
  238.  
  239.               use English;
  240.               $ofh = select(OUTF);
  241.               $FORMAT_NAME     = "My_Other_Format";
  242.               $FORMAT_TOP_NAME = "My_Top_Format";
  243.               select($ofh);
  244.  
  245.           But you still have those funny _s_e_l_e_c_t()s.  So just use the
  246.           FileHandle module.  Now, you can access these special
  247.           variables using lower-case method names instead:
  248.  
  249.               use FileHandle;
  250.               format_name     OUTF "My_Other_Format";
  251.               format_top_name OUTF "My_Top_Format";
  252.  
  253.           Much better!
  254.  
  255.      NNNNOOOOTTTTEEEESSSS
  256.           Since the values line may contain arbitrary expression (for
  257.           at fields, not caret fields), you can farm out any more
  258.  
  259.  
  260.  
  261.      Page 4                                          (printed 6/30/95)
  262.  
  263.  
  264.  
  265.  
  266.  
  267.  
  268.      PPPPEEEERRRRLLLLFFFFOOOORRRRMMMM((((1111)))) UUUUNNNNIIIIXXXX SSSSyyyysssstttteeeemmmm VVVV ((((RRRReeeelllleeeeaaaasssseeee 0000....0000 PPPPaaaattttcccchhhhlllleeeevvvveeeellll 00000000)))) PPPPEEEERRRRLLLLFFFFOOOORRRRMMMM((((1111))))
  269.  
  270.  
  271.  
  272.           sophisticated processing to other functions, like _s_p_r_i_n_t_f()
  273.           or one of your own.  For example:
  274.  
  275.               format Ident =
  276.                   @<<<<<<<<<<<<<<<
  277.                   &commify($n)
  278.               .
  279.  
  280.           To get a real at or caret into the field, do this:
  281.  
  282.               format Ident =
  283.               I have an @ here.
  284.                       "@"
  285.               .
  286.  
  287.           To center a whole line of text, do something like this:
  288.  
  289.               format Ident =
  290.               @|||||||||||||||||||||||||||||||||||||||||||||||
  291.                       "Some text line"
  292.               .
  293.  
  294.           There is no builtin way to say "float this to the right hand
  295.           side of the page, however wide it is."  You have to specify
  296.           where it goes.  The truly desperate can generate their own
  297.           format on the fly, based on the current number of columns,
  298.           and then _e_v_a_l() it:
  299.  
  300.               $format  = "format STDOUT = \n";
  301.                        . '^' . '<' x $cols . "\n";
  302.                        . '$entry' . "\n";
  303.                        . "\t^" . "<" x ($cols-8) . "~~\n";
  304.                        . '$entry' . "\n";
  305.                        . ".\n";
  306.               print $format if $Debugging;
  307.               eval $format;
  308.               die $@ if $@;
  309.  
  310.           Which would generate a format looking something like this:
  311.  
  312.            format STDOUT =
  313.            ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
  314.            $entry
  315.                    ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<~~
  316.            $entry
  317.            .
  318.  
  319.           Here's a little program that's somewhat like _f_m_t(1):
  320.  
  321.            format =
  322.            ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< ~~
  323.            $_
  324.  
  325.  
  326.  
  327.      Page 5                                          (printed 6/30/95)
  328.  
  329.  
  330.  
  331.  
  332.  
  333.  
  334.      PPPPEEEERRRRLLLLFFFFOOOORRRRMMMM((((1111)))) UUUUNNNNIIIIXXXX SSSSyyyysssstttteeeemmmm VVVV ((((RRRReeeelllleeeeaaaasssseeee 0000....0000 PPPPaaaattttcccchhhhlllleeeevvvveeeellll 00000000)))) PPPPEEEERRRRLLLLFFFFOOOORRRRMMMM((((1111))))
  335.  
  336.  
  337.  
  338.            .
  339.  
  340.            $/ = '';
  341.            while (<>) {
  342.                s/\s*\n\s*/ /g;
  343.                write;
  344.            }
  345.  
  346.  
  347.           FFFFooooooootttteeeerrrrssss
  348.  
  349.           While $FORMAT_TOP_NAME contains the name of the current
  350.           header format, there is no corresponding mechanism to
  351.           automatically do the same thing for a footer.  Not knowing
  352.           how big a format is going to be until you evaluate it is one
  353.           of the major problems.  It's on the TODO list.
  354.  
  355.           Here's one strategy:  If you have a fixed-size footer, you
  356.           can get footers by checking $FORMAT_LINES_LEFT before each
  357.           _w_r_i_t_e() and print the footer yourself if necessary.
  358.  
  359.           Here's another strategy; open a pipe to yourself, using
  360.           open(MESELF, "|-") (see the open() entry in the _p_e_r_l_f_u_n_c
  361.           manpage) and always _w_r_i_t_e() to MESELF instead of STDOUT.
  362.           Have your child process postprocesses its STDIN to rearrange
  363.           headers and footers however you like.  Not very convenient,
  364.           but doable.
  365.  
  366.           AAAAcccccccceeeessssssssiiiinnnngggg FFFFoooorrrrmmmmaaaattttttttiiiinnnngggg IIIInnnntttteeeerrrrnnnnaaaallllssss
  367.  
  368.           For low-level access to the formatting mechanism.  you may
  369.           use _f_o_r_m_l_i_n_e() and access $^A (the $ACCUMULATOR variable)
  370.           directly.
  371.  
  372.           For example:
  373.  
  374.               $str = formline <<'END', 1,2,3;
  375.               @<<<  @|||  @>>>
  376.               END
  377.  
  378.               print "Wow, I just stored `$^A' in the accumulator!\n";
  379.  
  380.           Or to make an _s_w_r_i_t_e() subroutine which is to _w_r_i_t_e() what
  381.           _s_p_r_i_n_t_f() is to _p_r_i_n_t_f(), do this:
  382.  
  383.  
  384.  
  385.  
  386.  
  387.  
  388.  
  389.  
  390.  
  391.  
  392.  
  393.      Page 6                                          (printed 6/30/95)
  394.  
  395.  
  396.  
  397.  
  398.  
  399.  
  400.      PPPPEEEERRRRLLLLFFFFOOOORRRRMMMM((((1111)))) UUUUNNNNIIIIXXXX SSSSyyyysssstttteeeemmmm VVVV ((((RRRReeeelllleeeeaaaasssseeee 0000....0000 PPPPaaaattttcccchhhhlllleeeevvvveeeellll 00000000)))) PPPPEEEERRRRLLLLFFFFOOOORRRRMMMM((((1111))))
  401.  
  402.  
  403.  
  404.               use English;
  405.               use Carp;
  406.               sub swrite {
  407.                   croak "usage: swrite PICTURE ARGS" unless @ARG;
  408.                   local($ACCUMULATOR);
  409.                   formline(@ARG);
  410.                   return $ACCUMULATOR;
  411.               }
  412.  
  413.               $string = swrite(<<'END', 1, 2, 3);
  414.            Check me out
  415.            @<<<  @|||  @>>>
  416.            END
  417.               print $string;
  418.  
  419.  
  420.      WWWWAAAARRRRNNNNIIIINNNNGGGG
  421.           During the execution of a format, only global variables are
  422.           visible, or dynamically-scoped ones declared with _l_o_c_a_l().
  423.           Lexically scoped variables declared with _m_y() are _N_O_T
  424.           available, as they are not considered to reside in the same
  425.           lexical scope as the format.
  426.  
  427.  
  428.  
  429.  
  430.  
  431.  
  432.  
  433.  
  434.  
  435.  
  436.  
  437.  
  438.  
  439.  
  440.  
  441.  
  442.  
  443.  
  444.  
  445.  
  446.  
  447.  
  448.  
  449.  
  450.  
  451.  
  452.  
  453.  
  454.  
  455.  
  456.  
  457.  
  458.  
  459.      Page 7                                          (printed 6/30/95)
  460.  
  461.  
  462.  
  463.